package com.work.cos.domain.vo;

import java.io.Serializable;
import java.util.List;

/**
 * 类功能说明：通用处理结果
 *
 * @author yang
 */
public class ComServiceResVo implements Serializable
{

     /**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -3952291005137360577L;

	/**
     * 数量
     */
    public static final int   COUNT_ZERO       = 0;

    /**
     * 返回结果 <br>
     * <p>
     * false : 异常 true : 成功
     * <p>
     * 默认=0 : 异常
     */
    private boolean           result           = false;

    /**
     * 消息
     */
    private String            message;

    /**
     * 返回状态
     *
     *
     */
    private int               status;

    /**
     * 件数
     */
    private long              count            = 0;

    /**
     * 结果实体
     */
    private Object            entity;

    /**
     * 结果集合
     */
    private List<?>           list;

    /**
     * 结果成功，返回entity
     *
     *
     * @param entity
     */
    public static ComServiceResVo returnEntity(Object entity)
    {
        return createComServiceResVo(true, Status.SUCCESS.getCode(), null, null, entity, null);
    }

    /**
     * 结果成功，返回不分页list
     *
     *
     * @param data list
     */
    public static ComServiceResVo returnList(List<?> data)
    {
        return  returnPagedList(data, data == null ? null : Long.valueOf(data.size()));
    }

    /**
     * return pageabled list 结果成功，返回分页LIst
     *
     * @param data
     */
    public static ComServiceResVo returnPagedList(List<?> data, Long count)
    {
        if(count == null || count.intValue() == COUNT_ZERO){
            return createComServiceResVo(true, Status.SUCCESS.getCode(), "暂时没有相关的信息喔", count, null, data);
        }else{
            return createComServiceResVo(true, Status.SUCCESS.getCode(), null, count, null, data);
        }
    }

    /**
     * 业务处理成功
     */
    public static ComServiceResVo returnSuccessStatus()
    {
        return createComServiceResVo(true, Status.SUCCESS.getCode(), null, null, null, null);
    }

    /**
     * 业务处理失败
     */
    public static ComServiceResVo returnErrorMessage(String message)
    {
        return createComServiceResVo(false, Status.BAD_REQUEST.getCode(), message, null, null, null);
    }

    /**
     * 返回业务处理结果状态
     *
     * @param result
     * @param status
     * @param message
     * @return
     */
    public static ComServiceResVo returnStatus(boolean result, int status, String message)
    {
        return createComServiceResVo(result, status, message, null, null, null);
    }

    /**
     * construct 业务处理成功或失败
     * @param result
     * @param status
     * @param message
     * @param count
     * @param entity
     * @param list
     */
    public static ComServiceResVo returnALL(boolean result, int status, String message, long count, Object entity,
                                        List<?> list)
    {

        return createComServiceResVo(result, status, message, count, entity, list);
    }


    /**
     *  所有结果手动设置
     * <p>
     *
     * @param result
     * @param status
     * @param message
     * @param count
     * @param entity
     * @param list
     */
    private static ComServiceResVo createComServiceResVo(boolean result, int status, String message, Long count, Object entity, List<?> list)
    {

        ComServiceResVo vo = new ComServiceResVo();
        vo.result = result;
        vo.status = status;

        if (message == null)
        {

            if (result)
            {
                vo.message = Status.SUCCESS.getDescription();
            }
            else
            {
                vo.message = "操作失败";
            }
        }
        else
        {
            vo.message = message;
        }

        if (count == null)
        {

            if (null != list)
            {
                vo.count = list.size();
            }
            else
            {
                vo.count = COUNT_ZERO;
            }
        }
        else
        {
            vo.count = count;
        }

        vo.entity = entity;
        vo.list = list;

        return vo;
    }


    /**
     * 返回状态定义
     */
    public enum Status {

        /*成功*/
        SUCCESS(0, "操作成功"),
        /*系统维护*/
        SERVER_MAINTAIN(-1, "status.serverMaintain"),
        /*客户端需要升级*/
        APP_NEED_UPGRADE(-2, "status.appNeedUpgread"),
        /*服务器接口错误*/
        SERVICE_ERROR(-3, "status.serviceError"),
        /*客户端目前没权限访问*/
        UNAUTHORIZED(-4, "status.unauthorized"),
        /*非法客户端*/
        INVALID_CLIENT(-5, "status.invalidClient"),
        /*乐观锁异常*/
        OPTIMISTIC_LOCK(-6, "status.optimisticLock"),

        //------------- 通用 ----------------------------
        /*业务参数不符合要求*/
        BAD_REQUEST(1, "status.badRequest"),

        /*上传token生成异常.*/
        UPLOAD_EXCEPTION(2 , "status.uploadException"),

        /* 操作太频繁 */
        TOOMUCH_OPERATOR(3, "status.tooMuchOperator"),


        /* 暂无数据 */
        NO_DATA(4, "com.cannotFind"),



        //------- 手机 -------------
        /*手机号码不能为空*/
        PHONE_NULL(2000 , "status.phoneNull"),
        /*手机号码格式不正确*/
        INVALID_PHONE(2001, "status.invalidPhone"),
        /*该号码已经注册*/
        REGISTER_PHONE(2002 , "ur.phoneExist"),


        //------------ 用户 ------------------------
        /*密码不能为空*/
        PASSWORD_NULL(2005 , "status.passwordNull"),
        /*该用户不存在*/
        USER_NONENTITY(2006 , "status.userNonentity"),
        /*用户名或密码错误*/
        INVALID_USERNAME_PASSWORD(2007 , "status.invalidUsernamePassword"),
        /* 该用户未绑定手机号*/
        UNBOUNDED_PHONE(2008 , "status.unboundedPhone"),
        /*用户状态异常*/
        STATUS_EXCEPTION(2009 , "status.statusException"),

        /* 签名异常*/
        SIGN_EXCEPTION(2024 , "status.signException");



        private final int code;
        private final String description;

        private Status(int _code, String _description) {
            this.description = _description;
            this.code = _code;
        }

        public String getDescription() {
            return description;
        }

        public int getCode() {
            return code;
        }


    }














    public boolean isResult()
    {
        return result;
    }

    public void setResult(boolean result)
    {
        this.result = result;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    public int getStatus()
    {
        return status;
    }

    public void setStatus(int status)
    {
        this.status = status;
    }

    public long getCount()
    {
        return count;
    }

    public void setCount(long count)
    {
        this.count = count;
    }

    public Object getEntity()
    {
        return entity;
    }

    public void setEntity(Object entity)
    {
        this.entity = entity;
    }

    public List<?> getList()
    {
        return list;
    }

    public void setList(List<?> list)
    {
        this.list = list;
    }

}
